var alarms = [];
var stationId;
var table;
var columns = ['序号','告警时间','电压','变电站','线路','保护','告警内容','中断时长','异常定位'];
function init(){
	$('#start').val( $('#startTime').val());
	$('#end').val( $('#endTime').val());
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView:2,
		autoclose : true,
		endDate: new Date(),
		todayHighlight:true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView:2,
		autoclose : true,
		endDate: new Date(),
		pickerPosition:'bottom-left',
		todayHighlight:true
	});
	stationId = $('#stationId').val();
	loadData();
}
function loadData(){
	var s = $('#start').val();
	var e = $('#end').val();
	$.getJSON("/rest/protectAlarm/getProtectInterruptAlarmByStationId/"
			+s+"/"+e+"/"+stationId,function(result){
		alarms = result;
		alarms.sort(sortAlarms);
		$("title").html(alarms[0]['stationName']+"  保护通信中断");
		loadHtml();
	});
}
function sortAlarms(obj1,obj2){
	//主要按照lastAlarmTime排序，次要按照eventStart排序
	var t1 = obj1.lastAlarmTime?obj1.lastAlarmTime:0;
	t1 = t1.time?t1.time:t1;
	var t2 = obj2.lastAlarmTime?obj2.lastAlarmTime:0;
	t2 = t2.time?t2.time:t2;
	return t2-t1;
}
function query(){
	loadData();
}
function loadHtml(){
	if(table){
		table.getTable().destroy();
	}
	var opt = MyTable.getDefaultOptions();
	opt.paging=true;
	opt.scrollY = "200px";
	opt.pageLength = 30;
	opt.lengthChange = true;
	opt.searching = true;
	table = new MyTable("alarmTable", columns, opt);
	var datas = [];
	for(var i in alarms){
		var f = alarms[i];
		var channels = '<span style="color:gray;">无通道告警信息</span>';
		if(f.channelAlarms && f.channelAlarms.length>0){
			channels = '<ul>';
			for(var j in f.channelAlarms){
				var a = f.channelAlarms[j];
				channels += '<li>'+a.alarmChannel+'</br>'+a.alarmObject+'</br><span style="color:red;">'+a.name+'</span></li>';
			}
			channels += '</ul>';
		}
		var durationTime = f.durationTime?f.durationTime:0+'ms';
		durationTime = '<span style="color:red;">'+durationTime+'</span>';
		datas.push([Number(i)+1,time(f.eventStart),str(f.protectName),str(f.alarmMsg),
		            durationTime,str(f.lineName), str(f.stationName),str(f.voltage),
		            channels]);
	}
	table.setDataSet(datas).show();
}
function str(v){
	return v?v:'';
}
function time(v){
	if(!v){
		return '';
	}else{
		return new Date(v.time?v.time:v).toLocaleString();
	}
}
